define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'client/index' + location.search,
                    add_url: 'client/add',
                    edit_url: 'client/edit',
                    del_url: 'client/del',
                    multi_url: 'client/multi',
                    import_url: 'client/import',
                    table: 'client',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'site_id', title: __('站点名称'), formatter: Controller.api.formatter.site_name,searchList: {
                                1:'星湖路养老服务站',
                                2:'洪湖东路养老服务站',
                                3:'天宫殿养老服务中心',
                                4:'玉兰路养老服务站',
                                5:'东湖南路养老服务站',
                                6:'东湖北路养老服务站',
                                7:'梧桐路养老服务站',
                                8:'丁香路养老服务站',
                            }},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'gender', title: __('Gender'),  formatter: Controller.api.formatter.gender_text,searchList: {1:'男',0:'女'}},
                        {field: 'id_number', title: __('Id_number'), operate: 'LIKE'},
                        {field: 'birth_date', title: __('Birth_date'), operate:false, addclass:'datetimerange', autocomplete:false},
                        {field: 'age', title: __('Age'), operate: false},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'contact_number', title: __('Contact_number'), operate: 'LIKE'},
                        {field: 'emergency_number', title: __('Emergency_number'), operate: false},
                        {field: 'life_ability', title: __('Life_ability'), operate: false},
                        {field: 'residency', title: __('Residency'), operate: false},
                        {field: 'personnel_type', title: __('Personnel_type'), operate: false},
                        {field: 'education_level', title: __('Education_level'), operate: false},
                        {field: 'work_unit', title: __('Work_unit'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate: false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate: false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter: {
                site_name: function (value, row, index) {
                    var name = '';
                    switch (row.site_id){
                        case 1:
                            name ='星湖路养老服务站';
                            break;
                        case 2:
                            name ='洪湖东路养老服务站';
                            break;
                        case 3:
                            name ='天宫殿养老服务中心';
                            break;
                        case 4:
                            name ='玉兰路养老服务站';
                            break;
                        case 5:
                            name ='东湖南路养老服务站';
                            break;
                        case 6:
                            name ='东湖北路养老服务站';
                            break;
                        case 7:
                            name ='梧桐路养老服务站';
                            break;
                        default:
                            name ='丁香路养老服务站';
                    }
                    return '<span style="white-space: nowrap;">'+ name +'</span>';
                },

                gender_text: function (value, row, index) {
                    if (row.gender === 1) {
                        return '<span style="white-space: nowrap;">男</span>';
                    } else if(row.gender === 2) {
                        return '<span style="white-space: nowrap;">女</span>';
                    } else{
                        return '<span style="white-space: nowrap;">未知</span>';
                    }
                },
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
